//
//  MessageInfoTableViewCell.m
//  BGM
//
//  Created by liuqin on 2018/12/4.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "MessageInfoTableViewCell.h"
#import "Common.h"
#import "UIImageView+WebCache.h"

@interface MessageInfoTableViewCell()

@property (nonatomic, strong)UIButton *textMore;
//@property (nonatomic, strong)UIButton *imageMore;

@property (nonatomic, strong)UIView *bottomView;


@end



@implementation MessageInfoTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self != nil) {
         self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [[UIView alloc]initWithFrame:CGRectZero];
        self.bgView.backgroundColor=[UIColor whiteColor];
        //v.layer.masksToBounds=YES;这行去掉
        self.bgView.layer.cornerRadius=10;
        self.bgView.layer.shadowColor= bgmsmallColor.CGColor;
        self.bgView.layer.shadowOffset=CGSizeMake(2, 5);
        self.bgView.layer.shadowOpacity=0.3;
        self.bgView.layer.shadowRadius=5;
        [self addSubview:self.bgView];

   
 
      
       
        
        
//
        self.typeImage = [[UIButton alloc]initWithFrame:CGRectMake(10, 5, 45, 45)];
        [self.bgView addSubview:self.typeImage];

        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 0, SCREENW-100, 55)];
        self.titleLabel.textColor = bgmmainColor;
        self.titleLabel.font = [UIFont systemFontOfSize:17];
        [self.bgView addSubview:self.titleLabel];

        UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, 55, SCREENW-30, 0.3)];
        line.alpha = 0.3;
        line.backgroundColor = [UIColor grayColor];
        [self.bgView addSubview:line];
        //55
        /////////////


        self.bottomView = [[UIView alloc]initWithFrame:CGRectZero];
        self.bottomView.backgroundColor = [UIColor clearColor];
         [self.bgView addSubview:self.bottomView];


        UILabel *line1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENW-30, 0.3)];
        line1.alpha = 0.3;
        line1.backgroundColor = [UIColor grayColor];
        [self.bottomView addSubview:line1];

        self.address = [[CommonBtn alloc]initWithFrame:CGRectMake(0, 0, 65, 40)];
        [self.address setImage:[UIImage imageNamed:@"adress"] forState:UIControlStateNormal];
        [self.bottomView addSubview:self.address];

        self.time = [[CommonBtn alloc]initWithFrame:CGRectMake(65, 0, 80, 40)];
        [self.time setImage:[UIImage imageNamed:@"pushTime"] forState:UIControlStateNormal];
        [self.bottomView addSubview:self.time];

        self.pinglun = [[CommonBtn alloc]initWithFrame:CGRectMake(SCREENW-30-100, 0, 50, 40)];
        [self.pinglun setImage:[UIImage imageNamed:@"pinglun"] forState:UIControlStateNormal];
        [self.bottomView addSubview:self.pinglun];
        [self.pinglun addTarget:self action:@selector(clickPinglun) forControlEvents:UIControlEventTouchUpInside];
        
        

        self.forward = [[CommonBtn alloc]initWithFrame:CGRectMake(SCREENW-30-50, 0, 50, 40)];
        [self.forward setImage:[UIImage imageNamed:@"zhuanfa"] forState:UIControlStateNormal];
        [self.bottomView addSubview:self.forward];
        //40
        ////////////////


        self.messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 70, 0, 0)];
        self.messageLabel.numberOfLines = 0;
        self.messageLabel.font = [UIFont systemFontOfSize:16];
        self.messageLabel.textColor = bgmgrayColor;
        [self.bgView addSubview:self.messageLabel];
        [self.messageLabel sizeToFit];


        self.textMore = [[UIButton alloc]initWithFrame:CGRectMake(self.bgView.frame.size.width-50, 0, 30, 20)];
        [self.textMore setTitle:@"全文" forState:UIControlStateNormal];
        [self.textMore setTitleColor:bgmyelloeColor forState:UIControlStateNormal];
        [self.textMore setImage:[UIImage imageNamed:@"moreDown"] forState:UIControlStateNormal];
        [self.textMore setTitle:@"收起" forState:UIControlStateSelected];
        [self.textMore setTitleColor:bgmyelloeColor forState:UIControlStateNormal];
        [self.textMore setImage:[UIImage imageNamed:@"moreUp"] forState:UIControlStateSelected];

        [self.textMore setImageEdgeInsets:UIEdgeInsetsMake(0, 35, 0, 0)];
        [self.textMore setTitleEdgeInsets:UIEdgeInsetsMake(0, -35, 0, 0)];

        [self.bgView addSubview:self.textMore];

        [self.textMore addTarget:self action:@selector(clickTextMore:) forControlEvents:UIControlEventTouchUpInside];
        self.textMore.titleLabel.font = [UIFont systemFontOfSize:13];
        self.textMore.hidden = YES;

        self.bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0,SCREENW-30-30 , 120)];
        [self.bgView addSubview:self.bgImageView];
        self.bgImageView.userInteractionEnabled = YES;
    }
    return self;
}




- (void)sizeToFitMessage:(NSDictionary *)dic isPhoto:(BOOL) isPhoto isMe:(BOOL) isMe isPublic:(BOOL)isPublic{
    
    if (dic.count==0) {
        return;
    }
   
    [self cellViewWithValue:dic isPublic:isPublic];
   
    if (isMe) {
        float cellH =  SCREENH-kNavBarHeight-kStatusBarHeight-70-80-30;
        self.bgView.frame = CGRectMake(15, 15, SCREENW-30, cellH-15);
    }else{
        float cellH =  SCREENH-kNavBarHeight-kStatusBarHeight-70-80-100;
        self.bgView.frame = CGRectMake(15, 15, SCREENW-30, cellH-15);
    }

    
    
    NSString *message =self.messageLabel.text;
    UIFont * tfont = [UIFont systemFontOfSize:16];
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
    CGSize  actualsize =[message boundingRectWithSize:CGSizeMake(self.bgView.frame.size.width-30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
    
    self.bottomView.frame = CGRectMake(0, self.bgView.frame.size.height-40, self.bgView.frame.size.width, 40);

    if(!isPhoto){
        
        float  messageH_MAX = self.bgView.frame.size.height-40-55-40; //文本上下间距15
        float messageH = 0.0;
        if ( actualsize.height > messageH_MAX) {
            messageH = messageH_MAX;
            self.textMore.hidden = NO;
            self.textMore.selected = NO;
        }else{
            messageH = actualsize.height;
        }
        self.messageLabel.frame = CGRectMake(15, 65, actualsize.width, messageH);
        self.textMore.frame = CGRectMake(self.bgView.frame.size.width-80, self.messageLabel.frame.origin.y+self.messageLabel.frame.size.height, 60, 20);
        
    }
    else
    {
        
        float imageH = (SCREENW-60-(10*4))/3;
        float messageH_MAX = self.bgView.frame.size.height-imageH-40-55  -15- 15 -  30;
        float messageH = 0.0;
        
        if ( actualsize.height > messageH_MAX) {
            messageH = messageH_MAX;
            self.textMore.hidden = NO;
            self.textMore.selected = NO;
            self.messageLabel.frame = CGRectMake(15, 65, self.bgView.frame.size.width-30, messageH);
            self.bgImageView.frame = CGRectMake(15,self.messageLabel.frame.origin.y+self.messageLabel.frame.size.height+15, self.bgView.frame.size.width-30, self.bgView.frame.size.height-self.messageLabel.frame.size.height-self.messageLabel.frame.origin.y-55-40);
        }else{
            messageH = actualsize.height;
            self.messageLabel.frame = CGRectMake(15, 65, self.bgView.frame.size.width-30, messageH);
            self.bgImageView.frame = CGRectMake(15,self.messageLabel.frame.origin.y+self.messageLabel.frame.size.height+15, self.bgView.frame.size.width-30, self.bgView.frame.size.height-self.messageLabel.frame.size.height-self.messageLabel.frame.origin.y-55-40);
        }
        
        
        
        CGRect imageBgRect = self.bgImageView.frame;
        
        if (imageBgRect.size.height<imageH) {
            imageBgRect.size.height = imageH;
            self.bgImageView.frame = imageBgRect;
        }
          self.textMore.frame = CGRectMake(self.bgView.frame.size.width-75,self.bgImageView.frame.origin.y+self.bgImageView.frame.size.height+3, 60, 20);
        
        
        NSArray *imageArray ;
        if(isPublic){
            imageArray = [dic objectForKey:@"pimages"];
        }else{
            imageArray = [dic objectForKey:@"images"];
        }
        
         int imageMax = 3;
        if(self.bgImageView.frame.size.height>imageH*3+20){
            imageMax = (int)imageArray.count;
        }else if (self.bgImageView.frame.size.height>imageH*2+10){
            imageMax = 6;
        }else{
            imageMax = 3;
        }
 
        if (imageArray.count>imageMax) {
            self.textMore.hidden = NO;
        }
        
        int max = imageMax<imageArray.count?imageMax:(int)imageArray.count;
        
        for(int i = 0;i<=max-1;i++) {
                int x = i%3;
                int y = i/3;
                UIImageView *photoImage = [[UIImageView alloc]initWithFrame:CGRectMake((imageH+10)*x,y*(imageH+10), imageH, imageH)];
                photoImage.layer.masksToBounds = YES;
                photoImage.layer.cornerRadius = 5;
                photoImage.contentMode = UIViewContentModeScaleAspectFill;
                NSString *image = [imageArray objectAtIndex:i];
                NSString *str = [NSString stringWithFormat:@"%@",image];
                [photoImage sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"imageNormal"]];
                [self.bgImageView addSubview:photoImage];

                photoImage.tag = 100+i;
                photoImage.userInteractionEnabled = YES;
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImage:)];
                [photoImage addGestureRecognizer:tap];
        }

    }
    
}


- (void)sizeToFitMessageALL:(NSDictionary *)dic isPhoto:(BOOL) isPhoto isMe:(BOOL) isMe isPublic:(BOOL)isPublic{

    if (dic.count==0) {
        return;
    }
    [self cellViewWithValue:dic isPublic:isPublic];
    
    NSString *message = self.messageLabel.text;//[NSString stringWithFormat:@"%@",[dic objectForKey:@"publishContent"]];
    UIFont * tfont = [UIFont systemFontOfSize:16];
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
    CGSize  actualsize =[message boundingRectWithSize:CGSizeMake(SCREENW-30-30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
  

    if(!isPhoto){
        self.bgView.frame = CGRectMake(15, 15, SCREENW-30, actualsize.height+40+55+40);
        self.textMore.hidden = NO;
        self.textMore.selected = YES;
        self.messageLabel.frame = CGRectMake(15, 65, actualsize.width, actualsize.height);
        self.textMore.frame = CGRectMake(self.bgView.frame.size.width-80, self.messageLabel.frame.origin.y+self.messageLabel.frame.size.height, 60, 20);
        self.bottomView.frame = CGRectMake(0, self.bgView.frame.size.height-40, self.bottomView.frame.size.width, 40);
    }
    
    else
    {
        float H;
        self.textMore.hidden = NO;
        self.textMore.selected = YES;
        if (isMe) {
            float cellH =  SCREENH-kNavBarHeight-kStatusBarHeight-70-80;
            self.bgView.frame = CGRectMake(15, 15, SCREENW-30, cellH);
            H = cellH-15;
        }else{
            float cellH =  SCREENH-kNavBarHeight-kStatusBarHeight-70-80-100;
            self.bgView.frame = CGRectMake(15, 15, SCREENW-30, cellH);
             H = cellH-15;
        }
        
        NSArray *imageArray;
        if(isPublic){
            imageArray = [dic objectForKey:@"pimages"];

        }else{
            imageArray = [dic objectForKey:@"images"];
        }
        
        int mod = imageArray.count%3;
        int row = (int)imageArray.count/3;
        row = row + (mod>0?1:0);
        
        float imageH = (SCREENW-60-(10*4))/3;
        float messageH_MAX = H-imageH-130;
        float messageH = 0.0;
        if ( actualsize.height > messageH_MAX) {
            messageH = actualsize.height;
            self.bgImageView.frame = CGRectMake(15, messageH + 80, self.bgView.frame.size.width-30, (imageH+10)*row);
        }else{
            
            messageH = actualsize.height;
            self.bgImageView.frame = CGRectMake(15, messageH + 80, self.bgView.frame.size.width-30, (imageH+10)*row);
        }
        self.messageLabel.frame = CGRectMake(15, 65, actualsize.width, actualsize.height);

        
        self.bgView.frame = CGRectMake(15, 15, SCREENW-30, self.bgImageView.frame.size.height+self.bgImageView.frame.origin.y+70);
        
        self.textMore.frame = CGRectMake(self.bgView.frame.size.width-75, self.bgImageView.frame.origin.y+self.bgImageView.frame.size.height, 60, 20);
        self.bottomView.frame = CGRectMake(0, self.bgView.frame.size.height-40, self.bottomView.frame.size.width, 40);
        
        
        for(int i = 0;i<imageArray.count;i++) {
            int x = i%3;
            int y = i/3;
            UIImageView *photoImage = [[UIImageView alloc]initWithFrame:CGRectMake((imageH+10)*x,y*(imageH+10), imageH, imageH)];
            photoImage.layer.masksToBounds = YES;
            photoImage.layer.cornerRadius = 5;
            photoImage.contentMode = UIViewContentModeScaleAspectFill;
            NSString *image = [imageArray objectAtIndex:i];
            NSString *str = [NSString stringWithFormat:@"%@",image];
            [photoImage sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"imageNormal"]];
            [self.bgImageView addSubview:photoImage];

            photoImage.tag = 100+i;
            photoImage.userInteractionEnabled = YES;

            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImage:)];
            [photoImage addGestureRecognizer:tap];

        }

     }
}





- (void)cellViewWithValue:(NSDictionary *)dic isPublic:(BOOL)isPublic{
    if (isPublic) {
        NSString *message = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pcontent"]];
        self.messageLabel.text = message;
        [self.address setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"pcity"]] forState:UIControlStateNormal];
        [self.time setTitle:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",[dic objectForKey:@"ptime"]] ] forState:UIControlStateNormal];
        [self.forward setTitle:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",[dic objectForKey:@"followSize"]]] forState:UIControlStateNormal];
        [self.pinglun setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"commentsSize"]] forState:UIControlStateNormal];
        self.titleLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"ptitle"]];
        NSString *imageStr = [NSString stringWithFormat:@"push_%@",[NSString stringWithFormat:@"%@",[dic objectForKey:@"ptype"]]];
        [self.typeImage setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
    }
    else
    {
        NSString *message = [NSString stringWithFormat:@"%@",[dic objectForKey:@"publishContent"]];
        self.messageLabel.text = message;
        [self.address setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"publishCity"]] forState:UIControlStateNormal];
        [self.time setTitle:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",[dic objectForKey:@"publishTime"]] ] forState:UIControlStateNormal];
        [self.forward setTitle:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",[dic objectForKey:@"publishFowordCount"]]] forState:UIControlStateNormal];
        [self.pinglun setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"commentSize"]] forState:UIControlStateNormal];
        self.titleLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"publishTitle"]];
        NSString *imageStr = [NSString stringWithFormat:@"push_%@",[NSString stringWithFormat:@"%@",[dic objectForKey:@"typeId"]]];
        [self.typeImage setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
    }
}

+ (float)returnCellHight:(NSDictionary *)dic isPhtot:(BOOL) isPhoto isMe:(BOOL) isMe isPublic:(BOOL)isPublic{
    if (dic.count==0) {
        return 0;
    }
    if (isMe) {
        return SCREENH-kNavBarHeight-kStatusBarHeight-70-80-30+10;
    }else{
        return SCREENH-kNavBarHeight-kStatusBarHeight-70-80-100+10;
    }
}

+ (float)returnCellHightALL:(NSDictionary *)dic isPhtot:(BOOL) isPhoto isMe:(BOOL) isMe isPublic:(BOOL)isPublic{

    if (dic.count==0) {
        return 0;
    }
    
    NSString *message;
    
    if(isPublic){
        message = [NSString stringWithFormat:@"%@",[dic objectForKey:@"pcontent"]];

    }else{
       message = [NSString stringWithFormat:@"%@",[dic objectForKey:@"publishContent"]];
    }
    
   
    UIFont * tfont = [UIFont systemFontOfSize:16];
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
    CGRect bgview = CGRectMake(15, 15, SCREENW-30, 420);
    CGSize  actualsize =[message boundingRectWithSize:CGSizeMake(bgview.size.width-30, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
    if(!isPhoto){
        return actualsize.height+40+55+40 + 15;
    }else{

        float H = 0;
        float returnH = 0;
        if (isMe) {
            float cellH =  SCREENH-kNavBarHeight-kStatusBarHeight-70-80;
             H = cellH-15;
        }else{
            float cellH =  SCREENH-kNavBarHeight-kStatusBarHeight-70-80-100;
             H = cellH-15;
        }
        NSArray *imageArray ;
        if (isPublic) {
            imageArray = [dic objectForKey:@"pimages"];
        }else{
            imageArray = [dic objectForKey:@"images"];
        }
        
        int mod = imageArray.count%3;
        int row = (int)imageArray.count/3;
        row = row + (mod>0?1:0);
        
        float imageH = (SCREENW-60-(10*4))/3;
        float messageH_MAX = H-imageH-130;
        float messageH = 0.0;
        
        if ( actualsize.height > messageH_MAX) {
            messageH = actualsize.height;
        }else{
//            messageH = messageH_MAX;
            messageH = actualsize.height;

        }
        returnH = 55+messageH+(imageH+20)*row;
       
        return  returnH+60 + 40+20;
        
    }

}







-(void)clickTextMore:(UIButton *)btn{
    
    [self.meseeageInfoDeleagte clickAll];
}


- (void)clickImage:(UIGestureRecognizer *)gesture{
    
    UIImageView *imageView = (UIImageView *)gesture.view;
    int tag = (int)imageView.tag;
    [self.meseeageInfoDeleagte clickImage:tag];
}
- (void)clickPinglun{
    [self.meseeageInfoDeleagte clickCommen];
}

@end
